package com.ny.service.impl;

import com.ny.mapper.FitnessProductMapper;
import com.ny.mapper.ProductPurchaseMapper;
import com.ny.domain.FitnessProductDO;
import com.ny.domain.ProductPurchaseDO;
import com.ny.dto.FitnessProductDTO;
import com.ny.dto.PurchaseRecordDTO;
import com.ny.service.ProductService;
import java.lang.Boolean;
import java.lang.Integer;
import java.lang.Override;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ny.exception.BusinessException;
import java.util.Date;

/**
 * 商品管理的实现
 *
 * @author MSI-NB
 * @date 2025-03-08 13:14:38
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private FitnessProductMapper fitnessProductMapper;

    @Autowired
    private ProductPurchaseMapper productPurchaseMapper;

    @Override
    public List<FitnessProductDO> getAllFitnessProducts() {
        List<FitnessProductDO> fitnessProducts = fitnessProductMapper.selectAll();
        return fitnessProducts;
    }

    @Override
    public FitnessProductDO getFitnessProductById(Integer productId) {
        FitnessProductDO fitnessProductDO = fitnessProductMapper.selectById(productId);
        if (fitnessProductDO == null) {
            throw new BusinessException("商品不存在");
        }
        return fitnessProductDO;
    }

    @Override
    public Boolean addFitnessProduct(FitnessProductDTO fitnessProductDTO) {
        FitnessProductDO fitnessProductDO = new FitnessProductDO();
        fitnessProductDO.setProductName(fitnessProductDTO.getProductName());
        fitnessProductDO.setProductDescription(fitnessProductDTO.getProductDescription());
        fitnessProductDO.setPrice(fitnessProductDTO.getPrice());
        fitnessProductDO.setCreateBy("admin");
        fitnessProductDO.setCreateTime(new Date());
        int result = fitnessProductMapper.insert(fitnessProductDO);
        return result > 0;
    }

    @Override
    public Boolean updateFitnessProduct(FitnessProductDTO fitnessProductDTO) {
        FitnessProductDO existingProduct = fitnessProductMapper.selectById(fitnessProductDTO.getProductId());
        if (existingProduct == null) {
            throw new BusinessException("商品不存在");
        }
        FitnessProductDO fitnessProductDO = new FitnessProductDO();
        fitnessProductDO.setProductId(fitnessProductDTO.getProductId());
        fitnessProductDO.setProductName(fitnessProductDTO.getProductName());
        fitnessProductDO.setProductDescription(fitnessProductDTO.getProductDescription());
        fitnessProductDO.setPrice(fitnessProductDTO.getPrice());
        fitnessProductDO.setUpdateBy("admin");
        fitnessProductDO.setUpdateTime(new Date());
        int result = fitnessProductMapper.updateById(fitnessProductDO);
        return result > 0;
    }

    @Override
    public Boolean deleteFitnessProduct(Integer productId) {
        FitnessProductDO existingProduct = fitnessProductMapper.selectById(productId);
        if (existingProduct == null) {
            throw new BusinessException("商品不存在");
        }
        int result = fitnessProductMapper.deleteById(productId);
        return result > 0;
    }

    @Override
    public List<ProductPurchaseDO> getAllPurchaseRecordsByMemberId(Integer memberId) {
        List<ProductPurchaseDO> purchaseRecords = productPurchaseMapper.selectAllByMemberId(memberId);
        return purchaseRecords;
    }

    @Override
    public ProductPurchaseDO getPurchaseRecordById(Integer purchaseId) {
        ProductPurchaseDO productPurchaseDO = productPurchaseMapper.selectById(purchaseId);
        if (productPurchaseDO == null) {
            throw new BusinessException("购买记录不存在");
        }
        return productPurchaseDO;
    }

    @Override
    public Boolean addPurchaseRecord(PurchaseRecordDTO purchaseRecordDTO) {
        ProductPurchaseDO productPurchaseDO = new ProductPurchaseDO();
        productPurchaseDO.setMemberId(purchaseRecordDTO.getMemberId());
        productPurchaseDO.setProductId(purchaseRecordDTO.getProductId());
        productPurchaseDO.setQuantity(purchaseRecordDTO.getQuantity());
        productPurchaseDO.setTotalAmount(purchaseRecordDTO.getTotalAmount());
        productPurchaseDO.setPurchaseDate(new Date());
        productPurchaseDO.setCreateBy("admin");
        productPurchaseDO.setCreateTime(new Date());
        int result = productPurchaseMapper.insert(productPurchaseDO);
        return result > 0;
    }

    @Override
    public Boolean updatePurchaseRecord(PurchaseRecordDTO purchaseRecordDTO) {
        ProductPurchaseDO existingPurchase = productPurchaseMapper.selectById(purchaseRecordDTO.getPurchaseId());
        if (existingPurchase == null) {
            throw new BusinessException("购买记录不存在");
        }
        ProductPurchaseDO productPurchaseDO = new ProductPurchaseDO();
        productPurchaseDO.setPurchaseId(purchaseRecordDTO.getPurchaseId());
        if (purchaseRecordDTO.getQuantity() != null) {
            productPurchaseDO.setQuantity(purchaseRecordDTO.getQuantity());
        }
        if (purchaseRecordDTO.getTotalAmount() != null) {
            productPurchaseDO.setTotalAmount(purchaseRecordDTO.getTotalAmount());
        }
        productPurchaseDO.setUpdateBy("admin");
        productPurchaseDO.setUpdateTime(new Date());
        int result = productPurchaseMapper.updateById(productPurchaseDO);
        return result > 0;
    }

    @Override
    public Boolean deletePurchaseRecord(Integer purchaseId) {
        ProductPurchaseDO existingPurchase = productPurchaseMapper.selectById(purchaseId);
        if (existingPurchase == null) {
            throw new BusinessException("购买记录不存在");
        }
        int result = productPurchaseMapper.deleteById(purchaseId);
        return result > 0;
    }
}
